home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_08 / treu / egapixel.c < prev    next >
C/C++ Source or Header  |  1994-04-25  |  4KB  |  152 lines

  1. /******************************************************
  2.  * EGAPIXEL.C - this is a hardware-specific function
  3.  * to examine and fill EGA/VGA pixels.
  4.  *
  5.  * for TurboC V2.0
  6.  *
  7.  * by Anton Treuenfels
  8.  * last revision:  04/11/94
  9.  *
  10.  * references:
  11.  * Stevens, Roger T., "Graphics Programming in C",
  12.  *   M&T Books, 1988
  13.  * Wilton, Richard, "PC & PS/2 Video Systems",
  14.  *   Microsoft Press, 1987
  15.  *****************************************************/
  16.  
  17. #include "usrdef.h"
  18.  
  19. /*******************************
  20.  * Header Section - EGAPIXEL.H *
  21.  *******************************/
  22.  
  23. #ifndef SEEN_EGAPIX
  24. #define SEEN_EGAPIX
  25.  
  26. /* function prototype */
  27.  
  28. void egaflood(int, int, Boolean);
  29.  
  30. #endif
  31.  
  32. /*****************************
  33.  * Code Section - EGAPIXEL.C *
  34.  *****************************/
  35.  
  36. #include <stddef.h>
  37. #include <dos.h>
  38. #include <graphics.h>
  39.  
  40. #include "bgigrh.h"
  41. #include "flood.h"
  42. #include "uflood.h"
  43.  
  44. #define BRDRPIX WHITE
  45. #define BACKPIX LIGHTBLUE
  46. #define FOREPIX BLUE
  47.  
  48. /* screen dimension */
  49.  
  50. #define XMAX    639     /* highest supported by BGI */
  51.  
  52. /* video memory */
  53.  
  54. #define VBASE   0xa0000000L     /* base address */
  55. #define ROWSIZE 80              /* bytes per row */
  56. #define COLMASK 0xfff8          /* column mask */
  57. #define COLSHFT 3               /* column shift */
  58. #define PIXMASK 0x07            /* pixel select */
  59.  
  60. /* graphics controller */
  61.  
  62. #define SETGRH(R, V)    {outportb(0x3ce, (R)); \
  63.                          outportb(0x3cf, (V));}
  64.  
  65. #define COMP    2       /* color compare register */
  66. #define DFLTCMP 0x00
  67.  
  68. #define MODE    5       /* mode register */
  69. #define DFLTMOD 0x00
  70. #define R1W2    0x06    /* read mode 1, write mode 2 */
  71.  
  72. #define MASK    8       /* bitmask register */
  73. #define DFLTMSK 0xff
  74.  
  75. /* pixel masks and pattern */
  76.  
  77. static Byte pixMask[] = {0x80, 0x40, 0x20, 0x10,
  78.                          0x08, 0x04, 0x02, 0x01};
  79. static Byte patMask[] = {0xcc, 0xcc, 0x33, 0x33,
  80.                          0xcc, 0xcc, 0x33, 0x33};
  81.  
  82. #define PATMASK 0x07        /* pattern select */
  83.  
  84. /* screen variables */
  85.  
  86. static int currCol;         /* current column */
  87. static int currRow;         /* current row */
  88. static int colOffset;       /* column offset */
  89. static Byte far *rowPtr;    /* row offset */
  90. static Byte far *screenPtr; /* current screen byte */
  91. static Byte rowPattern;     /* row fill pattern */
  92.  
  93. /*****************************************************/
  94.  
  95. /* initialize current column and row */
  96.  
  97. static void initcurr(int ypos) {
  98.  
  99.     currCol = ~COLMASK;
  100.     currRow = ypos - 1;
  101. }
  102.  
  103. /* examine one pixel */
  104.  
  105. static int egapixel(int xpos, int ypos) {
  106.  
  107.     Byte bitmask;
  108.  
  109.     if (ypos != currRow) {
  110.         if ((ypos < 0) || (ypos > MaxYPos))
  111.             return(FALSE);
  112.         currRow = ypos;
  113.         rowPtr = (Byte far *)(VBASE + ypos * ROWSIZE);
  114.         screenPtr = rowPtr + colOffset;
  115.         rowPattern = patMask[ypos & PATMASK];
  116.     }
  117.  
  118.     if ((xpos & COLMASK) != currCol) {
  119.         if ((xpos < 0) || (xpos > XMAX))
  120.             return(FALSE);
  121.         currCol = xpos & COLMASK;
  122.         colOffset = xpos >> COLSHFT;
  123.         screenPtr = rowPtr + colOffset;
  124.     }
  125.  
  126.     bitmask = pixMask[xpos & PIXMASK];
  127.     if ((*screenPtr & bitmask) != 0)
  128.         return(FALSE);
  129.  
  130.     SETGRH(MASK, bitmask);
  131.     *screenPtr =
  132.          (rowPattern & bitmask) ? FOREPIX : BACKPIX;
  133.     return(TRUE);
  134. }
  135.  
  136. /* execute fill */
  137.  
  138. void egaflood(int xpos, int ypos, Boolean fastfill) {
  139.  
  140.     abspoint(&xpos, &ypos);
  141.     initcurr(ypos);
  142.     SETGRH(COMP, BRDRPIX);
  143.     SETGRH(MODE, R1W2);
  144.     if (fastfill)
  145.         (void)flood(xpos, ypos, egapixel);
  146.     else
  147.         (void)uflood(xpos, ypos, egapixel);
  148.     SETGRH(MASK, DFLTMSK);
  149.     SETGRH(MODE, DFLTMOD);
  150.     SETGRH(COMP, DFLTCMP);
  151. }
  152.